package cn.itsource.controller;

import cn.itsource.result.JSONResult;
import cn.itsource.template.TXCosTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

/**
 * @program fccar_tt
 * @Description
 * @Author TT
 * @Date 2024/09/12 18:18
 * @Version 1.0
 */
@RestController
@RequestMapping("/common")
public class CommonController {
    @Autowired
    private TXCosTemplate txCosTemplate;
    @PostMapping("/cos/upload")
    public JSONResult upload(@RequestParam("file")MultipartFile file) throws IOException {
        //这个方法获取的是上传文件的文件名  xxx.png
        String originalFilename = file.getOriginalFilename();
        //获取后缀名
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        //拼接文件名
        String fileName= UUID.randomUUID().toString()+suffix;
        return JSONResult.success(txCosTemplate.upload(file.getInputStream(),fileName));
    }
}
